package daoImp;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;


import util.DateUtil;

import dao.PhotoDAO;

public class PhotoDAOImpl implements PhotoDAO{
	private static PhotoDAOImpl uniqueInstance = new PhotoDAOImpl();
	private ConnectionPool pool = ConnectionPool.getInstance();
	public static PhotoDAOImpl getInstance() {
		return uniqueInstance;
	}

	private PhotoDAOImpl() {
	}

	public String getPhotoInfo(String trend, Date date) {
		String sql = null;
		String photoInfo = "";
		try {
			Connection connection;
			try {
				connection = pool.getConnection();
			} catch (Exception e) {
				e.printStackTrace();
				return photoInfo;
			}
			Statement stmt = connection.createStatement();
			String dateStr = DateUtil.getNewsDateStr(date);
			trend = trend.replaceAll("'", "\\\\'").replaceAll("\\)", "\\\\)").replaceAll("\\(", "\\\\(");
			sql = "select info from photoInfos where trend = '" + trend + "' and date = '" + dateStr +"';";
			ResultSet rs = stmt.executeQuery(sql);
			while (rs.next()) {
				photoInfo = rs.getString(1);
			}
			pool.returnConnection(connection);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return photoInfo;
	}

}
